<?php
function CreateItem($item)
{
	$EntryId=intval($item['EntryId']);
	$Required=$item['Required']?'<span class="ss-required-asterisk">*</span>':'';
	$QTitle=$item['QuestionTitle'];
	$QHelp=$item['HelpText'];
	$QDefaultValue=GetValue($EntryId);
	switch($item['EntryType'])
	{		
		case 0://文本
			return '
            <div class="errorbox-good">
                <div class="ss-item ss-item-required ss-text">
                    <div class="ss-form-entry">
                        <label class="ss-q-title" for="entry_'.$EntryId.'">
                           '.$QTitle.'
                            '.$Required.'</label>
                        <label class="ss-q-help" for="entry_'.$EntryId.'">
                            '.$QHelp.'</label>
                        <input type="text" name="entry.'.$EntryId.'.single" value="'.$QDefaultValue.'" class="ss-q-short" id="entry_'.$EntryId.'"></div>
                </div>
            </div>
            <br>';
			break;
		case 1://段落文本
			return '
            <div class="errorbox-good">
                <div class="ss-item  ss-paragraph-text">
                    <div class="ss-form-entry">
                        <label class="ss-q-title" for="entry_'.$EntryId.'">
                            '.$QTitle.'
                        '.$Required.'</label>
                        <label class="ss-q-help" for="entry_'.$EntryId.'">
                            '.$QHelp.'</label>
                        <textarea name="entry.'.$EntryId.'.single" rows="8" cols="75" class="ss-q-long" id="entry_'.$EntryId.'">'.$QDefaultValue.'</textarea></div>
                </div>
            </div>';
			break;
		case 2:	//单项选择
			$Choices=$item['Choices'];$list='';$i=0;
			$GoToPageEnabled=$item['GoToPageEnabled'];
			foreach($Choices as $Choice)
			{
				$i++;
				$Value=$Choice['Value'];
				$SelectedByDefault=$Choice['SelectedByDefault']?' checked ':' ';
				$ManulChecked=$Value==$QDefaultValue?' checked ':'';
				$Selected=$QDefaultValue==null?$SelectedByDefault:$ManulChecked;
				
				if($GoToPageEnabled)
					$GoToPage='
                                    <input type="hidden" name="'."GoToPage.$EntryId.$i".'" value="'.$Choice['GoToPage'].'">';
					else $GoToPage='';
				$list.='
                            <li class="ss-choice-item">
                                <label class="ss-choice-label">
                                    <input type="radio" name="entry.'.$EntryId.'.group"'.$Selected.'value="'.$Value.'"
                                        class="ss-q-radio" id="group_'.$EntryId.'_'.$i.'">'.$GoToPage.'
                                    '.$Value.'</label></li>';
			}
			return '
            <div class="errorbox-good">
                <div class="ss-item  ss-radio">
                    <div class="ss-form-entry">
                        <label class="ss-q-title" for="entry_'.$EntryId.'">
                             '.$QTitle.'
                        '.$Required.'</label>
                        <label class="ss-q-help" for="entry_'.$EntryId.'">
                            '.$QHelp.'</label>
                        <ul class="ss-choices">'.$list.'
                        </ul>
                    </div>
                </div>
            </div>';
			break;
		case 3://下拉列表
			$Choices=$item['Choices'];$list='';
			
			foreach($Choices as $Choice)
			{
				$Value=$Choice['Value'];
				$ManulChecked=$Value==$QDefaultValue?' selected ':'';
				$list.='
                            <option value="'.$Value.'" '.$ManulChecked.'>
                                '.$Value.'</option>';
			}
			return '
            <div class="errorbox-good">
                <div class="ss-item  ss-select">
                    <div class="ss-form-entry">
                        <label class="ss-q-title" for="entry_'.$EntryId.'">
                            '.$QTitle.'
                        '.$Required.'</label>
                        <label class="ss-q-help" for="entry_'.$EntryId.'">
                            '.$QHelp.'</label>
                        <select name="entry.'.$EntryId.'.single" id="entry_'.$EntryId.'">'.$list.'                         
                        </select></div>
                </div>
            </div>';
			break;
		case 4://多项选择
			$Choices=$item['Choices'];$list='';$i=0;
			$DefaultValues=split(' | ',$QDefaultValue);	
			foreach($Choices as $Choice)
			{
				$i++;
				$Value=$Choice['Value'];
				$ManulChecked=in_array($Value,$DefaultValues)?' checked ':'';
				$list.='
                            <li class="ss-choice-item">
                                <label class="ss-choice-label">
                                    <input type="checkbox" name="entry.'.$EntryId.'.group[]" value="'.$Value.'" '.$ManulChecked.'
                                        class="ss-q-checkbox" id="group_'.$EntryId.'_'.$i.'">                                    
                                    '.$Value.'</label></li>';
			}
			return '
            <div class="errorbox-good">
                <div class="ss-item  ss-checkbox">
                    <div class="ss-form-entry">
                        <label class="ss-q-title" for="entry_'.$EntryId.'">
                            '.$QTitle.'
                        '.$Required.'</label>
                        <label class="ss-q-help" for="entry_'.$EntryId.'">
                            '.$QHelp.'</label>
                        <ul class="ss-choices">'.$list.'                            
                        </ul>
                    </div>
                </div>
            </div>';
			break;
		case 5://评分
			$Choices=$item['Choices'];$list='';$list2='';$i=0;
			$MinLabel='';$MaxLabel='';
			
			foreach($Choices as $Choice)
			{
				$i++;
				$Value=$Choice['Value'];
				$ManulChecked=$Value==$QDefaultValue?' checked ':'';
				
				$list.='
                                <td class="ss-scalenumbers">
                                    <label class="ss-scalenumber" for="group_'.$EntryId.'_'.$i.'">
                                        '.$Value.'</label>
                                </td>';
				$list2.='
                                <td class="ss-scalerow">
                                    <input type="radio" name="entry.'.$EntryId.'.group" value="'.$Value.'" '.$ManulChecked.' class="ss-q-radio" id="group_'.$EntryId.'_'.$i.'">
                                </td>';
				if($i==1)
					$MinLabel=$Choice['Label'];
				$MaxLabel=$Choice['Label'];				
			}		
			return '
            <div class="errorbox-good">
                <div class="ss-item  ss-scale">
                    <div class="ss-form-entry">
                        <label class="ss-q-title" for="entry_'.$EntryId.'">
                            '.$QTitle.'
                        '.$Required.'</label>
                        <label class="ss-q-help" for="entry_'.$EntryId.'">
                            '.$QHelp.'</label>
                        <table border="0" cellpadding="5" cellspacing="0">
                            <tr>
                                <td class="ss-scalenumbers">
                                </td>'.$list.'                              
                                <td class="ss-scalenumbers">
                                </td>
                            </tr>
                            <tr>
                                <td class="ss-scalerow ss-leftlabel">
                                    '.$MinLabel.'
                                </td>'.$list2.'                               
                                <td class="ss-scalerow ss-rightlabel">
                                    '.$MaxLabel.'
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>';
			break;
		case 6://章节
			return '
            <div class="errorbox-good">
                <div class="ss-item  ss-section-header">
                    <div class="ss-form-entry">
                        <h2 class="ss-section-title">
                            '.$QTitle.'</h2>
                        <div class="ss-section-description ss-no-ignore-whitespace">
                            '.$QHelp.'</div>
                    </div>
                </div>
            </div>';
			break;
		case 7://网格
			$Choices=$item['Choices'];$list='';$list2='';$i=0;
			$MinLabel='';$MaxLabel='';
			$row=count($item['CompositeChildIds']);
			$col=count($item['Choices'])/$row;
			$ChildIds=$item['CompositeChildIds'];
			$width=48/$col;
			foreach($Choices as $Choice)
			{
				
				$i++;
				$Value=$Choice['Value'];
				$Label=$Choice['Label'];
				
				$ChildId=$ChildIds[intval(($i-1)/$col)];
				$_SESSION['header.'.$ChildId]="$QTitle-$Label";
				$ManulChecked=$Value==GetValue($ChildId)?' checked ':'';
				$N=($i-1)%$col+1;
				if($i<=$col)
				$list.='
                                    <td class="ss-gridnumbers" style="width: '.$width.'%;">
                                        <label class="ss-gridnumber">'.$Value.'</label>
                                    </td>';
				if($i%$col==1)
					$list2.='
                                <div class="errorbox-good">
                                    <tr class="ss-gridrow ss-grid-row-odd">
                                        <td class="ss-gridrow ss-leftlabel ss-gridrow-leftlabel" style="width: 32%;">
                                            '.$Label.'
                                        </td>
                                        <td class="ss-gridrow" style="width: 8%;">
                                        </td>';
				$list2.='
                                        <td class="ss-gridrow" style="width: '.$width.'%;">
                                            <input type="radio" name="entry.'.$ChildId.'.group" value="'.$Value.'" '.$ManulChecked.' class="ss-q-radio" id="group_'.$ChildId.'_'.$N.'">
                                        </td>';
				if($i%$col==0)					
					$list2.='
                                        <td class="ss-gridrow" style="width: 8%;">
                                        </td>
                                    </tr>
                                </div>';							
			}	
			return '
            <div class="ss-item  ss-grid">
                <div class="ss-form-entry">
                    <label class="ss-q-title" for="entry_'.$EntryId.'">
                        '.$QTitle.'
                    '.$Required.'</label>
                    <label class="ss-q-help" for="entry_'.$EntryId.'">
                       '.$QHelp.'</label>
                    <div class="errorbox-component errorbox-good">
                        <table border="0" cellpadding="'.($col+2).'" cellspacing="0">
                            <thead>
                                <tr>
                                    <td class="ss-gridnumbers" style="width: 32%;">
                                    </td>
                                    <td class="ss-gridnumbers" style="width: 8%;">
                                    </td>'.$list.'                                    
                                    <td class="ss-gridnumbers" style="width: 8%;">
                                    </td>
                                </tr>
                            </thead>
                            <tbody>'.$list2.'                               
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>';
			
			break;
		case 9:
			return '
            <div class="errorbox-good">
                <div class="ss-item  ss-page-break">
                    <div class="ss-form-entry">
                        <h2 class="ss-page-title">'.$QTitle.'</h2>
                        <div class="ss-page-description ss-no-ignore-whitespace">
                            '.$QHelp.'</div>
                    </div>
                </div>
            </div>';
			
			break;
	}
}

function FormHeader($Title,$Description)
{
	return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  <title>易表单 - '.$Title.'</title>
  <link href="html/form.css" type="text/css" rel="stylesheet">
</head>
<body class="ss-base-body" dir="ltr">
<div class="ss-form-container">
  <div class="ss-form-heading">
    <h1 class="ss-form-title">'.$Title.'</h1>
    <p></p>
    <div class="ss-form-desc ss-no-ignore-whitespace">'.$Description.'</div>
    <p></p>
    <hr class="ss-email-break" style="display: none;">
    <div class="ss-required-asterisk">*必填</div>
  </div>
  <div class="ss-form">';
}

function FormBody($Entries,$StartEntryId,$pageHistory)
{
	$htmlForm='';
	$nextButton='
          <input type="submit" name="save" value="提交 &raquo;">';	
	if($StartEntryId==-1)
	{
		$pageHistory='-1';
		$backButton='';		
		foreach($Entries as $EntryId=>$Entry)
		{
			if($Entry['EntryType']==9)
			{
				$nextButton='
          <input type="hidden" name="nextpage" value="'.$EntryId.'">
          <input type="submit" name="continue" value="继续 &raquo;">';
				break;
			}
			$htmlForm.=CreateItem($Entry)."\r\n            <br/>";			
		}
	}else
	{
		$backButton='
          <input type="submit" name="back" value="&laquo; 返回">';
		$NotFind=true;
		if($pageHistory!='')
			$pageHistory.=',';
		$pageHistory.=$StartEntryId;
		foreach($Entries as $EntryId=>$Entry)
		{
			if($EntryId!=$StartEntryId&&$NotFind)			
				continue;		
			
			if(!$NotFind&&$Entry['EntryType']==9)
			{
				$nextButton='
          <input type="hidden" name="nextpage" value="'.$EntryId.'">
          <input type="submit" name="continue" value="继续 &raquo;">';
				break;
			}
			$NotFind=false;
			$htmlForm.=CreateItem($Entry)."\r\n            <br/>";
			
		}
	}
	return '
    <form action="?id='.$_GET['id'].'" method="post" id="ss-form">
    <br>'.$htmlForm.'
      <div class="ss-item ss-navigate">
        <div class="ss-form-entry">
          <input type="hidden" name="pagehistory" value="'.$pageHistory.'">'.$backButton.$nextButton.'
          </div>
      </div>
    </form>';	
}

function FormFooter()
{
	return '
    <script type="text/javascript">
      (function ()
      {
        var divs = document.getElementById("ss-form").getElementsByTagName("div");
        var numDivs = divs.length;
        for (var j = 0; j < numDivs; j++)
        {
				if (divs[j].className == "errorbox-bad")
				{
					divs[j].lastChild.firstChild.lastChild.focus();
					return;
				}
        }
        for (var i = 0; i < numDivs; i++)
        {
				var div = divs[i];
				if (div.className == "ss-form-entry" && div.firstChild && div.firstChild.className == "ss-q-title") 
				{
					div.lastChild.focus();
					return;
				}
        }
		})();
    </script>
  </div>
  <div class="ss-footer">
    <div class="ss-attribution"></div>
  </div>
</div>
</body>
</html>';
}

function GetValue($EntryId)
{
	if(isset($_SESSION['TmpData'])&&isset($_SESSION['TmpData'][$EntryId]))
		return $_SESSION['TmpData'][$EntryId];	
	return null;
}